//
//  THGoodSpecChosedViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/17.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THGoodSpecChosedViewController.h"
#import "STTools_AutoAddView.h"
@interface THGoodSpecChosedViewController ()<STBWBrowserViewControllerDelegate>
@property(nonatomic, strong) STButton                     *stoneobs_ui_clearButton;
@property(nonatomic, strong) UIView                     *stoneobs_ui_customView;
@property(nonatomic, strong) STLabel                     *priceLable;/**<  */
@property(nonatomic, strong) STTools_AutoAddView                     *add_view;/**<  */

@property(nonatomic, strong) UIImageView                     *iconIMageView;/**<  */
@property(nonatomic, strong) STLabel                     *scoreLable;/**<  */
@end

@implementation THGoodSpecChosedViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self stoneobs_ui_configSubView];
    [self stoneobs_fun_st_autoAdjustAllResponder];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self stoneobs_fun_show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    self.view.backgroundColor = UIColor.clearColor;
    __weak typeof(self) weakSelf =  self;
    self.stoneobs_ui_clearButton = [[STButton alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME
                                                             title:nil
                                                        titleColor:nil
                                                         titleFont:0
                                                      cornerRadius:0
                                                   backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                                   backgroundImage:nil
                                                             image:nil];
    [weakSelf.stoneobs_ui_clearButton setClicAction:^(UIButton *sender) {
        
    }];
    [self.view addSubview:_stoneobs_ui_clearButton];
    
    self.stoneobs_ui_customView = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    self.stoneobs_ui_customView.centerX = UISCREEN_WIDTH / 2;
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    self.stoneobs_ui_customView.backgroundColor = [UIColor whiteColor];
    self.stoneobs_ui_customView.layer.cornerRadius = 10;
    self.stoneobs_ui_customView.clipsToBounds = YES;
    [self.view addSubview:self.stoneobs_ui_customView];
    
   
    NSString * surl = self.detailModel.img;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15,20, 100 , 100)];
    [imageView sd_setImageWithURL:self.detailModel.img.mj_url];
    if (self.detailModel.parameter.count) {
        NSArray * names = [self.detailModel.specs stoneobs_fun_arrayFromObjKeyName:@"obj.name"];
        NSInteger index = [names indexOfObject:self.detailModel.cus_selected_spec.name];
        NSString * url = self.detailModel.parameter[index];
        if (self.detailModel.parameter.count > (index)) {
            [imageView sd_setImageWithURL:url.mj_url];
            surl = url;
        }
       
        
    }
    self.iconIMageView = imageView;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = 4;
    imageView.clipsToBounds = YES;
    imageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        NSMutableArray * models = [NSMutableArray new];
        STBWPhotoModel * model = [STBWPhotoModel  new];
        model.originImageUrl = weakSelf.iconIMageView.sd_imageURL.description;
        [models addObject:model];
        STBWBrowserViewController * vc = [[STBWBrowserViewController alloc] initWithArray:models curentIndex:0];
        vc.shouldHideBottomView = YES;
        vc.delegate = weakSelf;
        vc.shouldHideTopView = YES;
        [weakSelf presentViewController:vc animated:NO completion:nil];
    }];
    [imageView addGestureRecognizer:tap];
    [self.stoneobs_ui_customView addSubview:imageView];
    
    STButton * closebutton = [[STButton alloc] initWithFrame:CGRectMake(0, 10, 44, 44)
                                                       title:nil
                                                  titleColor:nil
                                                   titleFont:0
                                                cornerRadius:0
                                             backgroundColor:nil
                                             backgroundImage:nil
                                                       image:[UIImage imageNamed:@"关闭"]];
    closebutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    closebutton.right = UISCREEN_WIDTH  - 10;
    [closebutton setClicAction:^(UIButton *sender) {
        
        if (weakSelf.onSlectedDidChangeSpecHandle) {
            weakSelf.onSlectedDidChangeSpecHandle();
        }
        [weakSelf stoneobs_fun_dismiss];
    }];
    [self.stoneobs_ui_customView addSubview:closebutton];
    
    CGFloat top = 30;
    {{
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(130, top, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                text:[NSString stringWithFormat:@"￥%@",self.detailModel.cus_selected_spec_price]
                                                           textColor:FlatRed
                                                                font:20
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom + 5;
        stoneobs_ui_label.attributedText = [stoneobs_ui_label.text stoneobs_fun_st_convertAttributeStringWithKeyWord:@"￥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
        self.priceLable = stoneobs_ui_label;
    }};
    {{
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(130, top, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                text:[NSString stringWithFormat:@"库存:%@",self.detailModel.stock]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom + 20;
        
    }};
    
    {{
        
//        integral
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(130, top, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                text:[NSString stringWithFormat:@"赠送积分:%@",self.detailModel.cus_selected_spec.integral]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom + 20;
        self.scoreLable = stoneobs_ui_label;
        self.scoreLable.text = [NSString stringWithFormat:@"赠送积分:%@",self.detailModel.cus_selected_spec.integral];
    }};
    
    
    
    {{
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                text:self.detailModel.spec_name
                                                           textColor:UIColor.blackColor
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
        top = stoneobs_ui_label.bottom + 20;
        
    }};
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, 250)];
    [self.stoneobs_ui_customView addSubview:scrollView];
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    top = scrollView.bottom + 15;
    CGFloat scrolltop = 0;
    ({
        NSMutableArray * names = [self.detailModel.specs stoneobs_fun_arrayFromObjKeyName:@"obj.name"].mutableCopy;

        
        STTools_TagMenuView * menuview = [[STTools_TagMenuView alloc] initWithFrame:CGRectMake(15, scrolltop, STONEOBS_UI_UISCREEN_WIDTH - 30, 10)];
        menuview.allArray = names;
        NSString * name = self.detailModel.cus_selected_spec.name;
        if (!name.length) {
            name = @"";
        }
        menuview.chosedArray = @[name];
        
        menuview.buttonBoderColor = UIColor.clearColor;
        menuview.buttonSelectedBoderColor = WH_UICOLOR_FROM_RGBA(0x37CF34);
        
        menuview.buttonBackGroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
        menuview.buttonSelctedBackGroundColor = WH_UICOLOR_FROM_RGBA(0xD9FED8);
        
        menuview.butTitleColor = WH_UICOLOR_FROM_RGBA(0x666666);
        menuview.butTitleSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
        [scrollView addSubview:menuview];
        scrolltop = menuview.bottom + 10;
        [menuview setOnSlectedTagView:^(STTools_TagMenuView *tagView, NSString *title, NSInteger index) {
            weakSelf.detailModel.cus_selected_spec = weakSelf.detailModel.specs[index];
            weakSelf.priceLable.text = [NSString stringWithFormat:@"￥%@",self.detailModel.cus_selected_spec_price];
            weakSelf.priceLable.attributedText = [weakSelf.priceLable.text stoneobs_fun_st_convertAttributeStringWithKeyWord:@"￥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
            
            if (weakSelf.detailModel.parameter.count) {
                NSArray * names = [weakSelf.detailModel.specs stoneobs_fun_arrayFromObjKeyName:@"obj.name"];
                NSInteger index = [names indexOfObject:weakSelf.detailModel.cus_selected_spec.name];
                NSString * url = weakSelf.detailModel.parameter[index];
                if (weakSelf.detailModel.parameter.count > (index)) {
                    [imageView sd_setImageWithURL:url.mj_url];
                }
                
            }
            weakSelf.scoreLable.text = [NSString stringWithFormat:@"赠送积分:%@",weakSelf.detailModel.cus_selected_spec.integral];
        }];
        
    });
    
    if (self.detailModel.type_detail.count > 0) {
        {{
            STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, scrolltop, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                    text:self.detailModel.type_name
                                                               textColor:UIColor.blackColor
                                                                    font:15
                                                             isSizetoFit:NO
                                                           textAlignment:NSTextAlignmentLeft];
            [scrollView addSubview:stoneobs_ui_label];
            stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
            scrolltop = stoneobs_ui_label.bottom + 20;
            
        }};
        ({
            NSArray * names = self.detailModel.type_detail;
            STTools_TagMenuView * menuview = [[STTools_TagMenuView alloc] initWithFrame:CGRectMake(15, scrolltop, STONEOBS_UI_UISCREEN_WIDTH - 30, 10)];
            menuview.allArray = names;
            menuview.chosedArray = @[self.detailModel.cus_type_selcted];
            
            menuview.buttonBoderColor = UIColor.clearColor;
            menuview.buttonSelectedBoderColor = WH_UICOLOR_FROM_RGBA(0x37CF34);
            
            menuview.buttonBackGroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
            menuview.buttonSelctedBackGroundColor = WH_UICOLOR_FROM_RGBA(0xD9FED8);
            
            menuview.butTitleColor = WH_UICOLOR_FROM_RGBA(0x666666);
            menuview.butTitleSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
            [scrollView addSubview:menuview];
            scrolltop = menuview.bottom + 10;
            [menuview setOnSlectedTagView:^(STTools_TagMenuView *tagView, NSString *title, NSInteger index) {
                weakSelf.detailModel.cus_type_selcted = weakSelf.detailModel.type_detail[index];
                weakSelf.priceLable.text = [NSString stringWithFormat:@"￥%@",self.detailModel.cus_selected_spec_price];
                weakSelf.priceLable.attributedText = [weakSelf.priceLable.text stoneobs_fun_st_convertAttributeStringWithKeyWord:@"￥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
                weakSelf.scoreLable.text = [NSString stringWithFormat:@"赠送积分:%@",weakSelf.detailModel.cus_selected_spec.integral];
            }];

        });
        
        
    }
    
    scrollView.contentSize = CGSizeMake(0, scrolltop + 10);
    [scrollView setScrollEnabled:YES];
    
    
    {{
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, STONEOBS_UI_UISCREEN_WIDTH - 120 - 80, 20)
                                                                text:@"数量"
                                                           textColor:UIColor.blackColor
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
        top = stoneobs_ui_label.bottom + 60;
        
        STTools_AutoAddView * add_view = [[STTools_AutoAddView alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
        add_view.centerY = stoneobs_ui_label.centerY;
        add_view.right = UISCREEN_WIDTH - 10;
        [self.stoneobs_ui_customView addSubview:add_view];
        add_view.textFiled.userInteractionEnabled = YES;
        self.add_view = add_view;
        add_view.textFiled.text = @(self.detailModel.cus_selected_spec.cus_num).description;
        [add_view setAutoaddViewDidChangeHandle:^(STTools_AutoAddView *autoAddView, NSString *didChangeText) {
            
            weakSelf.detailModel.cus_selected_spec.cus_num = weakSelf.add_view.textFiled.text.intValue;
            weakSelf.priceLable.text = [NSString stringWithFormat:@"￥%@",weakSelf.detailModel.cus_selected_spec_price];
            weakSelf.priceLable.attributedText = [weakSelf.priceLable.text stoneobs_fun_st_convertAttributeStringWithKeyWord:@"￥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];

        }];
        
        
    }};
    if (STTools_UserManger.defult.stoneobs_fun_get_loginedUser.level.intValue == 3) {
        {{
            
            STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, STONEOBS_UI_UISCREEN_WIDTH - 30, 20)
                                                                    text:[NSString stringWithFormat:@"企业用户需要购买%@数量即可享受优惠价格",self.detailModel.business_num]
                                                               textColor:FlatRed
                                                                    font:13
                                                             isSizetoFit:NO
                                                           textAlignment:NSTextAlignmentLeft];
            [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
            top = stoneobs_ui_label.bottom + 60;
        }};
    }
    if (STTools_UserManger.defult.stoneobs_fun_get_loginedUser.level.intValue == 2) {
        {{
            
            STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, STONEOBS_UI_UISCREEN_WIDTH - 30, 20)
                                                                    text:[NSString stringWithFormat:@"小店用户需要购买%@数量即可享受优惠价格",self.detailModel.shop_num]
                                                               textColor:FlatRed
                                                                    font:13
                                                             isSizetoFit:NO
                                                           textAlignment:NSTextAlignmentLeft];
            [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
            top = stoneobs_ui_label.bottom + 60;
        }};
    }
    CGFloat witdh = (STONEOBS_UI_UISCREEN_WIDTH - 30 - 15) / 2;
    
    STButton * stoneobs_ui_car_button = [[STButton alloc] initWithFrame:CGRectMake(15, top, witdh, 40)
                                                                  title:@"加入购物车"
                                                             titleColor:UIColor.whiteColor
                                                              titleFont:14
                                                           cornerRadius:20
                                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                                        backgroundImage:nil
                                                                  image:nil];
    stoneobs_ui_car_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.stoneobs_ui_customView addSubview:stoneobs_ui_car_button];
    [stoneobs_ui_car_button setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_action_onsetced_add_car];
    }];
    stoneobs_ui_car_button.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleLeftToRight withFrame:stoneobs_ui_car_button.bounds andColors:@[WH_UICOLOR_FROM_RGBA(0xFF8B59),WH_UICOLOR_FROM_RGBA(0xFB692A)]];
    
    STButton * stoneobs_ui_buy_button = [[STButton alloc] initWithFrame:CGRectMake(15, top, witdh, 40)
                                                                  title:@"立刻购买"
                                                             titleColor:UIColor.whiteColor
                                                              titleFont:14
                                                           cornerRadius:20
                                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                                        backgroundImage:nil
                                                                  image:nil];
    stoneobs_ui_buy_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.stoneobs_ui_customView addSubview:stoneobs_ui_buy_button];
    [stoneobs_ui_buy_button setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_action_onsetced_buy_now];
    }];
    stoneobs_ui_buy_button.right = UISCREEN_WIDTH - 10;
    [TMUtils stoneobs_fun_makeViewToThemeGrdualColor:stoneobs_ui_buy_button];
    
    
    self.stoneobs_ui_customView.height = stoneobs_ui_buy_button.bottom + 20 + self.stoneobs_ui_customView.layer.cornerRadius ;
    if (@available(iOS 11.0, *)) {
        self.stoneobs_ui_customView.height = self.stoneobs_ui_customView.height + UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
    } else {
        // Fallback on earlier versions
    }
    
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    
}

- (void)stoneobs_action_onsetced_add_car{
    __weak typeof(self) weakSelf =  self;
    weakSelf.detailModel.cus_selected_spec.cus_num = weakSelf.add_view.textFiled.text.intValue;
    [THHomeDataController stoneobs_fun_add_car:self.detailModel.g_id num:@(self.detailModel.cus_selected_spec.cus_num).description
                                     type_name:self.detailModel.cus_type_selcted
                                       spec_id:self.detailModel.cus_selected_spec.s_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
        
    }];
}
- (void)stoneobs_action_onsetced_buy_now{
    __weak typeof(self) weakSelf =  self;
    weakSelf.detailModel.cus_selected_spec.cus_num = weakSelf.add_view.textFiled.text.intValue;
    [weakSelf stoneobs_fun_dismissWithHandle:^{
        THCommitOrderViewController * vc = THCommitOrderViewController.new;
        vc.detailModel = self.detailModel;
        vc.is_pt_and_i_create = YES;
        [self pushViewController:vc animated:YES];
    }];
}
#pragma mark --STBWBrowserViewControllerDelegate
- (UIView*)browserControllerDidScrollToIndexpath:(NSIndexPath *)indexPath model:(STBWPhotoModel *)model controller:(STBWBrowserViewController *)controller{
    
    return   self.iconIMageView;
}
- (void)rightBarActionFromController:(STBWBrowserViewController *)controller currentIndexPath:(NSIndexPath *)curentIndexpath{
    
    
}

- (void)stoneobs_fun_show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
        self.stoneobs_ui_customView.bottom = UISCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)stoneobs_fun_dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)stoneobs_fun_dismissWithHandle:(void(^)(void))handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}

@end
